//Stephen Erb Application GUI

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;

public class Language extends Person implements ActionListener
{

        private String language;
        private String based;
        private String proficiency;
        private String comments;
        JButton okE;
        JButton closeE;
        JFrame l;
        JFrame b;
        JFrame p;
        JFrame c;
        JTextField languageT;
        JTextField basedT;
        JTextField proficiencyT;
        JTextField commentsT;
    
        public Language() throws Exception {
            language = "";
            based = "";
            proficiency = "";
            comments = "";
            setLayout(new GridLayout(10, 4, 5, 5));
            JLabel languageL = new JLabel("Language: ");
            languageT = new JTextField(25);
            ((AbstractDocument)languageT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
            JLabel basedL = new JLabel("Based On: ");
            basedT = new JTextField(12);
            ((AbstractDocument)basedT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
            JLabel proficiencyL = new JLabel("Proficiency: ");
            proficiencyT = new JTextField(25);
            ((AbstractDocument)proficiencyT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
            JLabel commentsL = new JLabel("Comments: ");
            commentsT = new JTextField(12);
            ((AbstractDocument)commentsT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
            okE = new JButton("OK");
            closeE = new JButton("Close");
            okE.setActionCommand("OK");
            closeE.setActionCommand("CLOSE");
            okE.addActionListener(this);
            closeE.addActionListener(this);
            add(languageL);
            add(languageT);
            add(basedL);
            add(basedT);
            add(proficiencyL);
            add(proficiencyT);
            add(commentsL);
            add(commentsT);
            add(okE);
            add(closeE);
        }
    
        public boolean validateLanguage() {
            boolean valid = true;
            boolean size = true;
            char[] temp = languageT.getText().trim().toCharArray();
            for (int i = 0; i < temp.length; i++) {
                if (temp[i] < 'A' || temp[i] > 'Z') {
                    if (temp[i] < 'a' || temp[i] > 'z') {
                        if (temp[i] != ' ')
                            valid = false;
                    }
                }
            }
            if (temp.length > 25 || temp.length == 0)
                size = false;
            if (valid == false || size == false) {
                l = new JFrame("Language Error Message Box");
                l.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close = new JButton("Close");
                close.setActionCommand("LANGUAGEERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL = new JLabel();
                if (valid == false && size == true) {
                    eL.setText("Only letters and spaces!");
                } else if (valid == false && size == false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                } else if (valid == true && size == false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                l.add(eL);
                l.add(close);
                l.setSize(400, 300);
                l.setLocationRelativeTo(null);
                l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                l.setVisible(true);
            }
            if (valid && size) {
                setLanguage(languageT.getText().trim());
            }
            return (valid && size);
        }
    
        public boolean validateBased() {
            boolean valid = true;
            boolean size = true;
            char[] temp = basedT.getText().trim().toCharArray();
            for (int i = 0; i < temp.length; i++) {
                if (temp[i] < 'A' || temp[i] > 'Z') {
                    if (temp[i] < 'a' || temp[i] > 'z') {
                        if (temp[i] != ' ')
                            valid = false;
                    }
                }
            }
            if (temp.length > 25 || temp.length == 0)
                size = false;
            if (valid == false || size == false) {
                b = new JFrame("Based On Error Message Box");
                b.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close = new JButton("Close");
                close.setActionCommand("MANAGERERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL = new JLabel();
                if (valid == false && size == true) {
                    eL.setText("Only letters and spaces!");
                } else if (valid == false && size == false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                } else if (valid == true && size == false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                b.add(eL);
                b.add(close);
                b.setSize(400, 300);
                b.setLocationRelativeTo(null);
                b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                b.setVisible(true);
            }
            if (valid && size) {
                setBased(basedT.getText().trim());
            }
            return (valid && size);
        }
    
        public boolean proceedOnward() {
            boolean valid = true;
            if (validateLanguage() == false)
                valid = false;
            if (validateBased() == false)
                valid = false;
            if (validateProficiency() == false)
                valid = false;
            if (validateComments() == false)
                valid = false;
            if (validateFName() == false)
                valid = false;
            if (validateMName() == false)
                valid = false;
            if (validateLName() == false)
                valid = false;
            if (validateAge() == false)
                valid = false;
            if (validateSSN() == false)
                valid = false;
            return valid;
        }
    
        public boolean validateProficiency() {
            boolean valid = true;
            boolean size = true;
            char[] temp = proficiencyT.getText().toCharArray();
            for (int i = 0; i < temp.length; i++) {
                if (temp[i] < 'A' || temp[i] > 'Z') {
                    if (temp[i] < 'a' || temp[i] > 'z') {
                        if (temp[i] != ' ')
                            valid = false;
                    }
                }
            }
            if (temp.length> 25 || temp.length==0)
                size = false;
            if (valid == false || size == false) {
                p = new JFrame("Proficiency Error Message Box");
                p.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close = new JButton("Close");
                close.setActionCommand("PROFICIENCYERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL = new JLabel();
                if (valid == false && size == true) {
                    eL.setText("Only letters and spaces!");
                } else if (valid == false && size == false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                } else if (valid == true && size == false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                p.add(eL);
                p.add(close);
                p.setSize(400, 300);
                p.setLocationRelativeTo(null);
                p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                p.setVisible(true);
            }
            if (valid && size) {
                setProficiency(proficiencyT.getText());
            }
            return (valid && size);
        }
    
        public void initialize() {
            this.setTitle("Language Usage");
            remove(ok);
            remove(close);
            this.setSize(400, 300);
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    
        public boolean validateComments() {
            boolean valid = true;
            boolean size = true;
            char[] temp = commentsT.getText().trim().toCharArray();
            for (int i = 0; i < temp.length; i++) {
                if (temp[i] < 'A' || temp[i] > 'Z') {
                    if (temp[i] < 'a' || temp[i] > 'z') {
                        if (temp[i] != ' ')
                            valid = false;
                    }
                }
            }
            if (temp.length > 25 || temp.length == 0)
                size = false;
            if (valid == false || size == false) {
                c = new JFrame("Comments Error Message Box");
                c.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close = new JButton("Close");
                close.setActionCommand("COMMENTSERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL = new JLabel();
                if (valid == false && size == true) {
                    eL.setText("Only letters and spaces!");
                } else if (valid == false && size == false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                } else if (valid == true && size == false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                c.add(eL);
                c.add(close);
                c.setSize(400, 300);
                c.setLocationRelativeTo(null);
                c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                c.setVisible(true);
            }
            if (valid && size) {
                setComments(commentsT.getText().trim());
            }
            return (valid && size);
        }
    
        public void actionPerformed(ActionEvent event) {
                String command=event.getActionCommand();
                if(command=="OK")
                {
                        if(proceedOnward())
                        {
                            System.out.println(toString());
                            System.exit(0);
                            dispose();
                        }
                }
                else if(command=="CLOSE")
                {
                        dispose();
                        Choose c1=new Choose();
                        c1.initialize();
                }
                else if(command=="LANGUAGEERRORCLOSEBOX") 
                {
                        l.dispose();
                }
                else if(command=="BASEDERRORCLOSEBOX") 
                {
                        b.dispose();           
                }
                else if(command=="PROFICIENCYERRORCLOSEBOX") 
                {
                        p.dispose();
                }
                else if(command=="COMMENTSERRORCLOSEBOX") 
                {
                        c.dispose();
                }
        }
	public String getLanguage()
	{
		return language;
	}
	public String getBased()
	{
		return based;
	}
	public String getProficiency()
	{
		return proficiency;
	}
	public String getComments()
	{
		return comments;
	}
        public void setLanguage(String temp) {
                language=temp;
        }
        public void setBased(String temp) {
                based=temp;
        }
        public void setProficiency(String temp) {
                proficiency=temp;
        }
        public void setComments(String temp) {
                comments=temp;
        }
	@Override public String toString()
	{
		return "First Name: " + getFName() + "\nLast Name: " + getLName() + "\nMiddle Name: " + getMName() + "\nAge:" + getAge() + "\nSSN: " + stringSSN()+"\nLanguage: "+getLanguage()+"\nBased On: "+getBased()+"\nProficiceny: "+getProficiency()+"\nComments: "+getComments();
	}
}

